package com.coocaa.ops.api.v1.home.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.coocaa.ops.api.common.constant.ApiTabType;
import com.coocaa.ops.api.common.util.PluginUtil;
import com.coocaa.ops.api.v1.content.model.output.plugin.PluginInfo;
import com.coocaa.ops.api.v1.home.model.output.HomeContent;
import com.coocaa.ops.api.v1.home.model.output.HomeModule;
import com.coocaa.ops.api.v1.home.model.output.Icon;
import com.coocaa.ops.api.v1.home.model.output.SimpleTabModel;
import com.coocaa.ops.common.core.biz.homepage.entity.HomeModuleContentConfigInfo;
import com.coocaa.ops.common.core.biz.homepage.entity.HomepageInfoEntity;
import com.coocaa.ops.common.core.biz.module.entity.ModuleInfoEntity;
import com.coocaa.ops.common.core.biz.plugin.entity.PluginInfoEntity;
import com.coocaa.ops.common.core.biz.tab.constant.TabTypeConstants;
import com.coocaa.ops.common.core.biz.tab.entity.TabInfoEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tangminghong
 * @date 2020-06-23
 */
@Slf4j
public class HomeResultUtil {

    public static HomeContent convertHomeContent(List<HomeModuleContentConfigInfo> finalModuleList, HomepageInfoEntity homepageEntity) {
        HomeContent result = new HomeContent();
        result.setTotal(0);
        if (CollectionUtils.isEmpty(finalModuleList)) {
            return result;
        }
        result.setContents(new ArrayList<>());
        for (HomeModuleContentConfigInfo module : finalModuleList) {

            PluginInfo pluginInfo = PluginUtil.handleModulePluginInfo(module);

            HomeModule homeModule = new HomeModule();
            homeModule.setId(module.getId());
            homeModule.setTitle(module.getModuleTitle());
            homeModule.setType(module.getModuleType());
            homeModule.setFocus(module.getModuleIsFocus());

            homeModule.setTopTab(handleSimpleTabInfo(module.getTopTabInfo()));
            homeModule.setBottomTab(handleSimpleTabInfo(module.getBottomTabInfo()));

            homeModule.setIcon(handleIconInfo(module.getModuleIcon()));

            homeModule.setBg(module.getBackgroundPoster());
            homeModule.setBgColor(module.getBackgroundColor());

            homeModule.setPluginInfo(pluginInfo);

            homeModule.setAction(module.getOnclick());

            result.getContents().add(homeModule);
        }
        result.setTotal(result.getContents().size());
        result.setFocusColor(homepageEntity.getFocusColor());
        return result;
    }

    private static Icon handleIconInfo(String iconStr) {
        if (StringUtils.isBlank(iconStr)) {
            return null;
        }
        Icon icon = new Icon();
        try {
            JSONObject iconObj = JSON.parseObject(iconStr);
            String normal = iconObj.getString("default");
            String focus = iconObj.getString("focus");
            icon.setNormal(normal);
            icon.setFocus(focus);
        } catch (Exception e) {
            log.error("模块图标解析问题：" + e.getClass());
        }
        return icon;
    }

    private static SimpleTabModel handleSimpleTabInfo(TabInfoEntity tab) {
        if (tab == null || tab.getId() == null || tab.getId() == 0) {
            return null;
        }
        SimpleTabModel simpleTab = new SimpleTabModel();
        simpleTab.setTabId(tab.getId());
        simpleTab.setTabType(ApiTabType.convertFromCommonTabType(tab.getTabType()));
        if (TabTypeConstants.isPlugin(tab.getTabType())) {
            simpleTab.setPluginInfo(convertPluginInfo(tab.getPluginInfo()));
        }
        return simpleTab;
    }

    private static PluginInfo convertPluginInfo(PluginInfoEntity pluginEntity) {
        if (pluginEntity == null) {
            return null;
        }
        PluginInfo pluginInfo = new PluginInfo();

        pluginInfo.setType(pluginEntity.getType());
        pluginInfo.setPackageName(pluginEntity.getPackageName());
        pluginInfo.setCategory(pluginEntity.getCategory());
        pluginInfo.setData(pluginEntity.getData());
        pluginInfo.setHomeVersion(pluginEntity.getHomeVersion());
        pluginInfo.setPluginVersion(pluginEntity.getPluginVersion());

        String params = pluginEntity.getParams();
        if (StringUtils.isNotBlank(params)) {
            try {
                pluginInfo.setParams(JSONObject.parseObject(params));
            } catch (JSONException e) {
                //do nothing
            }
        }

        return pluginInfo;
    }
}
